package com.rayboo.example.java8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * forEach 示例
 *
 * @author ray
 * @since 2019/07/29
 *
 * <p>https://beginnersbook.com/2017/10/java-8-foreach/
 */
public class ForEachExample {

    public static void main(String[] args) {

        System.out.println("======== Java 8 – forEach to iterate a Map ========");
        Map<Integer, String> hmap = new HashMap<Integer, String>();
        hmap.put(1, "Monkey");
        hmap.put(2, "Dog");
        hmap.put(3, "Cat");
        hmap.put(4, "Lion");
        hmap.put(5, "Tiger");
        hmap.put(6, "Bear");
        /* forEach to iterate and display each key and value pair
         * of HashMap.
         */
        hmap.forEach((key,value)->System.out.println(key+" - "+value));
        /* forEach to iterate a Map and display the value of a particular
         * key
         */
        hmap.forEach((key,value)->{
            if(key == 4){
                System.out.println("Value associated with key 4 is: "+value);
            }
        });
        /* forEach to iterate a Map and display the key associated with a
         * particular value
         */
        hmap.forEach((key,value)->{
            if("Cat".equals(value)){
                System.out.println("Key associated with Value Cat is: "+key);
            }
        });

        System.out.println("======== Java 8 – forEach to iterate a List ========");
        List<String> fruits = new ArrayList<String>();
        fruits.add("Apple");
        fruits.add("Orange");
        fruits.add("Banana");
        fruits.add("Pear");
        fruits.add("Mango");
        //java8 expression in forEach Method
        fruits.forEach(System.out::println);

        System.out.println("======== Java 8 – forEach method to iterate a Stream ========");
        List<String> names = new ArrayList<String>();
        names.add("Maggie");
        names.add("Michonne");
        names.add("Rick");
        names.add("Merle");
        names.add("Governor");
        names.stream() //creating stream
                .filter(f->f.startsWith("M")) //filtering names that starts with M
                .forEach(System.out::println); //displaying the stream using forEach

        System.out.println("======== Java 8 – Stream forEachOrdered() Method Example ========");
        //forEach - the output would be in any order
        System.out.println("Print using forEach");
        names.stream()
                .filter(f->f.startsWith("M"))
                .parallel()
                .forEach(System.out::println);

        /* forEachOrdered - the output would always be in this order:
         * Maggie, Michonne, Merle
         */
        System.out.println("Print using forEachOrdered");
        names.stream()
                .filter(f->f.startsWith("M"))
                .parallel()
                .forEachOrdered(System.out::println);
    }
}
